var exec = require("child_process").exec;
var querystring = require("querystring") , fs = require("fs");

function start(response,postData){
	console.log("Request handle 'start' was called");
//	var content = "empty";
	
/*	function sleep(millSeconds){
		var startTime = new Date().getTime();
		while (new Date().getTime() < startTime + millSeconds);
	}
	sleep(5000);
*/
	var body = '<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/></head><body><form action="/upload" enctype="multipart/form-data" method="post"><input type="file" name="upload"><input type="submit" value="Upload file"/></form></body></html>'; 


//	exec("dir" , function(error , stdout, stderr){
//		content = stdout;
		response.writeHead(200, {"Content-Type" : "text/html"});
		response.write(body);
		response.end();
//	});
}

function upload(response,postData){
	console.log("Request handle 'upload' was called");
	response.writeHead(200, {"Content-Type" : "text/plain"});
	response.write("You've sent: " + querystring.parse(postData).text);
	
	response.end();
}

function show(response , postData){
	console.log("Request handle 'show' was called.");
	fs.readFile("D:/1.png" , "binary" , function(error,file){
		if(error){
			response.writeHead(500,{"Content-Type":"text/plain"});
			response.write(error + "\n");
			response.end();
		}else{
			response.writeHead(200 , {"Content-Type":"image/png"});
			response.write(file, "binary");
			response.end();
		}
	});
}

exports.start = start;
exports.upload = upload;
exports.show = show;